package com.lottery.im.module.login.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.lottery.im.R;
import com.lottery.im.base.BaseSwipeFragment;
import com.lottery.im.module.login.presenter.ResetPwdPresenter;
import com.lottery.im.module.login.view.ResetPwdView;
import com.lottery.im.util.StatusBarHelper;

/*
 * 重置密码页
 * */
public class ResetPwdFragment extends BaseSwipeFragment<ResetPwdPresenter> implements ResetPwdView {
  private final static String PHONE_KEY = "PHONE_KEY";
  private final static String CODE_KEY = "CODE_KEY";

  public static ResetPwdFragment create(String phone, String code) {
    ResetPwdFragment fragment = new ResetPwdFragment();
    Bundle b = new Bundle();
    b.putString(PHONE_KEY, phone);
    b.putString(CODE_KEY, code);
    fragment.setArguments(b);
    return fragment;
  }

  @BindView(R.id.tv_title) TextView mTvTitle;
  @BindView(R.id.et_pwd1) EditText mEtPwd1;
  @BindView(R.id.et_pwd2) EditText mEtPwd2;
  private String mPhone;
  private String mCode;

  @Override protected int getLayoutResId() {
    return R.layout.fragment_reset_pwd;
  }

  @Override protected void initPresenter() {
    presenter = new ResetPwdPresenter(getActivity(), this, this);
    presenter.init();
  }

  @Override public void initView() {
    StatusBarHelper.setStatusBarDarkMode(getActivity());
    mTvTitle.setText(R.string.forget_password_title);
    if (getArguments() == null) {
      popBackStack();
      return;
    } else {
      mPhone = getArguments().getString(PHONE_KEY);
      mCode = getArguments().getString(CODE_KEY);
    }
  }

  @OnClick({ R.id.iv_back, R.id.btn_next }) public void onViewClicked(View view) {
    switch (view.getId()) {
      case R.id.iv_back:
        popBackStack();
        break;
      case R.id.btn_next:
        resetPwd();
        break;
    }
  }

  private void resetPwd() {
    String pwd1 = mEtPwd1.getText().toString().trim();
    String pwd2 = mEtPwd2.getText().toString().trim();
    if (TextUtils.isEmpty(pwd1)) {
      showToast(R.string.hint_enter_password);
      return;
    }
    if (TextUtils.isEmpty(pwd2)) {
      showToast(R.string.hint_enter_password_again);
      return;
    }
    if (pwd1.length() < 6) {
      showToast(R.string.hint_password);
      return;
    }
    if (!pwd1.equals(pwd2)) {
      showToast(R.string.pwd_different);
      return;
    }
    presenter.resetPassword(mPhone, mCode, pwd1, pwd2);
  }

  /*
   *  重置密码完成
   * */
  @Override public void resetSuccess() {
    popBackStack();
  }
}
